package com.mo.trace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mo.trace.entity.History;
import com.mo.trace.entity.ProductAll;
import com.mo.trace.mapper.HistoryMapper;
import com.mo.trace.mapper.ProductAllMapper;
import com.mo.trace.mapper.ProductCategoryMapper;
import com.mo.trace.service.HistoryService;
import com.mo.trace.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * (History)表服务实现类
 *
 * @author MoYz
 * @since 2021-04-27 17:38:51
 */
@Service("historyService")
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Resource
    private HistoryMapper historyMapper;
    @Resource
    private ProductAllMapper productAllMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Resource
    private UserService userService;

    public HistoryServiceImpl() {
    }

    /**
     * 添 加 操 作
     */
    @Override
    public Boolean add(History history) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper
                .eq("user_id", history.getUserId())
                .eq("product_id", history.getProductId());
        History history1 = historyMapper.selectOne(wrapper);
        if (history1 != null) {
            history.setRemark(String.valueOf(new Date()));
            return historyMapper.updateById(history1) == 1;
        }
        return historyMapper.insert(history) == 1;
    }

    /**
     * 删 除 操 作
     */
    @Override
    public Boolean del(String id) {
        return historyMapper.deleteById(id) == 1;
    }

    /**
     * 修 改 操 作
     */
    @Override
    public Boolean update(History history) {
        return historyMapper.updateById(history) == 1;
    }

    /**
     * 模 糊 查 询 及 分 页
     */
    @Override
    public Page<History> query(Integer pages, String key) {
        Page<History> page = new Page<>(pages, 10);
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper
                .like("id", key).or()
                .like("user_id", key).or()
                .like("product_id", key).or()
                .like("remark", key);
        historyMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 模 糊 查 询 及 分 页
     */
    @Override
    public ArrayList<History> queryAll() {
        //System.out.println("SSSS" + userService.getLoggedUserInfo().getId());
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper
                .like("user_id", userService.getLoggedUserInfo().getId())
                .orderByDesc("update_time");
        ArrayList<History> histories = new ArrayList<>(historyMapper.selectList(wrapper));
        System.out.println("未改变数据：" + historyMapper.selectList(wrapper).toString());
        for (int i = 0; i < histories.size(); i++) {
            if (i == 100) break;
            String pid = histories.get(i).getProductId();
            //拿到商品类型
            QueryWrapper<ProductAll> wrapperPType = new QueryWrapper<>();
            wrapperPType.eq("id", pid);
            String type = productAllMapper.selectOne(wrapperPType).getType();
            System.out.println("type" + type);
            //通过商品类型确定ID
//            QueryWrapper<ProductCategory> wrapperPName = new QueryWrapper<>();
//            wrapperPName.like("type", type);
//            String PName = productCategoryMapper.selectOne(wrapperPName).getId();
            //将id放入List
            histories.get(i).setRemark(type);
        }
        System.out.println(histories);
        return histories;
    }
}